<?php

/**
 * UserAuthComponent handle if an user is authentificated an if he can visualize a project.
 *
 * @package default
 * @author Rémi Peron
 **/
class UserAuthComponent extends Object
{
    var $controller = true;

	var $myController = null;
	var $user = null;

	/**
	 * Function execute when loading a controller. It checks if the user is authentificated.
	 *
	 * @return void
	 * @author Rémi Peron
	 **/
    function startup(&$controller)
    {
        // This method takes a reference to the controller which is loading it.
        // Perform controller initialization here.
		$controller->set ('user', null);
		$this->myController = $controller;
		$controller->set('current_project_id', 0);
		if ($controller->action != 'login' && $controller->action != 'logout' ) {
			if ($controller->Session->check('User') == false) {

				$this->log("Denied Access", LOG_DEBUG);
//				pr($controller->params);
				if ($controller->params['controller'] == "users" && $controller->params['action'] == 'index') {
					$controller->Session->write('url', '/');
				} else {
					$controller->Session->write('url', $controller->params['url']['url']);
				}
					$controller->redirect('/users/login');
					$controller->Session->setFlash(__('flash_loggin_needed', true));
			} else {
				$this->user = $controller->Session->read('User');
				$controller->set ('user', $this->user['User']);
				$controller->set('roles', $this->user['Role']);
				$project_id = 1;
				if (isset($controller->params['projectId'])) {
					$project_id = $controller->params['projectId'];
					$controller->set('current_project_id', $controller->params['projectId']);
				}
				$model = $this->myController->modelClass;
				$result = $this->myController->$model->query("SELECT c.url_logo FROM companies AS c LEFT JOIN projects AS p ON p.company_id = c.id WHERE p.id = '".$project_id."'");
				/*
				 * If the user is linked with a company (all without admin user)
				 */
				if (count($result) != '0')
					$controller->set('logo', $result[0]['c']['url_logo']);
			}
		}
    }

	/**
	 * GetUser information
	 *
	 * @return the current user in session.
	 * @author Rémi Peron
	 */
	public function getUser() {
		return $this->user['User'];
	}

	/**
	 * This function Check if an user is allowed to see a project
	 *
	 * @param project id
	 * @return void
	 * @author Rémi Peron
	 **/
	public function checkPermission($project_id)
	{
		foreach ($this->user['Role'] as $obj) {
			if ($obj['project_id'] == $project_id) {
				return ;
			}
		}
		$this->myController->Session->setFlash('Vous n\'avez pas les droits pour voir ce projet');
		$this->myController->redirect('/users/');
	}
	
	/**
	 * This function check if the user have the permission for making something
	 *
	 * @param int Project_id
	 * @return boolean
	 * @author Arnaud Vuillaume
	 **/
	public function checkAcces($project_id, $acces_name)
	{
		$model = $this->myController->modelClass;
		foreach ($this->user['Role'] as $obj) {
			if ($obj['project_id'] == $project_id) {
				$status_id = $obj['status_id'];
				$results = $this->myController->$model->query("SELECT * FROM acces_rules WHERE name = '".$acces_name."'");
				return $results[0]['acces_rules']["$status_id"];
			}
		}
		return false;
	}

	/**
	 * This function check if the user is admin
	 *
	 * @return boolean
	 * @author Kévin Labesse
	 **/
	public function isAdmin()
	{
		if ($this->user['User']['rank'] == 1)
			return true;
		else
			return false;	
	}
	
	/**
	 * This function check if the user is Director of project
	 *
	 * @return boolean
	 * @author Kévin Labesse
	 **/
	public function isDP()
	{
		if ($this->user['User']['rank'] == 4)
			return true;
		else
			return false;	
	}

	/**
	 * This function check if the user is Team Leader
	 *
	 * @return boolean
	 * @author Kévin Labesse
	 **/
	public function isTL()
	{
		if ($this->user['User']['rank'] == 3)
			return true;
		else
			return false;	
	}
	
	/**
	 * This function check if the user is a Smile User
	 *
	 * @return boolean
	 * @author Kévin Labesse
	 **/
	public function isSmile()
	{
		if ($this->user['User']['company_id'] == 1)
			return true;
		else
			return false;	
	}
	
	/**
	 * This function check if the new rank is higher than the old one
	 *
	 * @author Kévin Labesse
	 **/
	public function checkRank($idUser, $rank)
	{
		$results = $this->myController->User->query("SELECT rank FROM users WHERE id = '".$idUser."'");
		$oldRank = $results[0]['users']['rank'];
		if ($oldRank == 1)
			return;
		if ($rank == 4 && $oldRank != 4){
			$this->myController->User->query("UPDATE users SET rank=4 WHERE id = '".$idUser."'");
			return;
		}
		if ($rank == 3 && ($oldRank != 4 || $oldRank != 3)){
			$this->myController->User->query("UPDATE users SET rank=3 WHERE id = '".$idUser."'");
			return;
		}
	}	
}
?>